/**
 * @(#)CodeEntitySymbolTable.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.IJavaProject;

import edu.pku.sei.pgie.beans.CodeEntity;

/**
 * @author HeLi
 */
public class CodeEntitySymbolTable {
    public static final int MAX_SIZE = 200;

    IJavaProject javaProject;

    Map<String, CodeEntityBundle> FQNMap;

    private static class CodeEntityBundle {
        CodeEntity entity;
        List<Object> bundle;
        public CodeEntityBundle(CodeEntity codeEntity) {
            bundle = new ArrayList<Object>();
            entity = codeEntity;
        }
        public CodeEntity getCodeEntity() {
            return entity;
        }

        public List<Object> getBundle() {
            return bundle;
        }

        public void addBundle(Object object) {
            bundle.add(object);
        }
    }

    public CodeEntitySymbolTable() {
        FQNMap = new HashMap<String, CodeEntityBundle>();
    }

    public boolean contains(String FQN) {
        return FQNMap.containsKey(FQN);
    }

    @SuppressWarnings("unchecked")
    public void put(String FQN, CodeEntity entity) {
        if (FQN == null || entity == null) {
            return;
        }
        try {
            FQNMap.put(FQN, new CodeEntityBundle(entity));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Long getCodeID(String FQN) {
        if (FQN == null)
            return null;
        CodeEntityBundle result = FQNMap.get(FQN);
        if (result == null)
            return null;
        return result.getCodeEntity().getCodeId();
    }

    public CodeEntity getCodeEntity(String FQN) {
        if (FQN == null)
            return null;
        CodeEntityBundle result = FQNMap.get(FQN);
        if(result == null)
            return null;
        return result.getCodeEntity();
    }

    public List<Object> getCodeEntityBundle(String FQN) {
        if (FQN == null)
            return null;
        CodeEntityBundle result = FQNMap.get(FQN);
        if(result == null)
            return null;
        return result.getBundle();
    }

}
